home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / WINDEV.ARC / WNTMSC.C < prev    next >
C/C++ Source or Header  |  1989-07-30  |  7KB  |  237 lines

  1. /* 
  2.  * Less frequently used functions for WNTERM
  3.  *
  4.  * Written by
  5.  * William S. Hall
  6.  * 3665 Benton Street, #66
  7.  * Santa Clara, CA 95051
  8. */
  9.  
  10. #define NOKANJI
  11. #define NOATOM
  12. #define NOSOUND
  13. #include <windows.h>
  14. #include <string.h>
  15. #include "ttycls.h"
  16. #include "wnterm.h"
  17.  
  18. /* local function declarations */
  19. static void NEAR SetCommInit(HWND hDlg);
  20. static BOOL NEAR SetCommValues(HWND hDlg);
  21.  
  22. /* procedure for the about box */
  23. BOOL FAR PASCAL AboutBoxProc(hDlg, message, wParam, lParam)
  24. HWND hDlg;
  25. unsigned message;
  26. WORD wParam;
  27. LONG lParam;
  28. {
  29.  
  30.     switch (message) {
  31.     case WM_INITDIALOG:    /* nothing to initialize */
  32.         break;
  33.     case WM_COMMAND:    /* system command */
  34.         switch (wParam) {
  35.         case IDOK:    /* respond only to these parameter */
  36.         case IDCANCEL:
  37.             EndDialog(hDlg,TRUE);
  38.             break;
  39.         default:
  40.             return FALSE;    /* windows will handle this */
  41.         }
  42.         break;
  43.  
  44.     default:
  45.         return FALSE;    /* and, windows will process this as well */
  46.     }
  47.     return TRUE;        /* if we process message, return TRUE */
  48. }
  49.  
  50. /* display a simple message box according to the string id */
  51. void ShowMessage(HWND hWnd, int msgnum)
  52. {
  53.  
  54.     char szMessage[80];
  55.  
  56.   /* get the string corresponding to msgnum */
  57.     LoadString(hInst, msgnum, (LPSTR)szMessage, sizeof(szMessage));
  58.     
  59.   /* put up this simple message box */
  60.     MessageBox(hWnd,(LPSTR)szMessage, (LPSTR)szAppName, 
  61.         MB_ICONASTERISK | MB_OK);
  62.  
  63. }
  64.  
  65. /* set the communications parameters selected by user */
  66. BOOL FAR PASCAL SetCommParams(hDlg, message, wParam, lParam)
  67. HWND hDlg;
  68. unsigned message;
  69. WORD wParam;
  70. LONG lParam;
  71. {
  72.  
  73.     int result;
  74.  
  75.     switch (message) {
  76.     case WM_INITDIALOG:        /* initialize the box parameters */
  77.         SetCommInit(hDlg);
  78.         break;
  79.  
  80.     case WM_COMMAND:    
  81.         switch (wParam) {
  82.         case IDD_BAUDRATE: /* if nothing in baudrate box, disable OK */
  83.             if (HIWORD(lParam) == EN_CHANGE) {
  84.                 result = (int)SendDlgItemMessage(hDlg,wParam,
  85.                         WM_GETTEXTLENGTH,0,0L);
  86.             EnableWindow(GetDlgItem(hDlg, IDOK), result);
  87.             }
  88.             break;
  89.  
  90.         case IDOK:    /* set comm parameters from values in box */
  91.             result = SetCommValues(hDlg);
  92.             if (result)
  93.                 EndDialog(hDlg, TRUE);
  94.             break;
  95.         
  96.         case IDCANCEL:        /* user wants no changes */
  97.             EndDialog(hDlg, FALSE);
  98.             break;
  99.  
  100.         case IDD_COM1:    /* highlight the button selected */
  101.         case IDD_COM2:
  102.             CheckRadioButton(hDlg, IDD_COM1, IDD_COM2, wParam);
  103.             break;
  104.  
  105.         case IDD_SEVENBITS:  /* highlight the button selected */
  106.         case IDD_EIGHTBITS:
  107.             CheckRadioButton(hDlg,IDD_SEVENBITS,IDD_EIGHTBITS,wParam);
  108.             break;
  109.  
  110.         case IDD_NOPARITY:   /* highlight the button selected */
  111.         case IDD_ODDPARITY:
  112.         case IDD_EVENPARITY:
  113.         case IDD_MARKPARITY:
  114.         case IDD_SPACEPARITY:
  115.             CheckRadioButton(hDlg,IDD_NOPARITY,IDD_SPACEPARITY,wParam);
  116.             break;
  117.  
  118.         default:
  119.             return FALSE;
  120.         }
  121.         break;
  122.  
  123.     default:
  124.         return FALSE;
  125.     }
  126.     return TRUE;
  127. }
  128.  
  129. /* read the communications DCB and initialize the dialog box accordingly */
  130. static void NEAR SetCommInit(HWND hDlg)
  131. {
  132.  
  133.   /* enter the baud rate into the baudrate edit control */
  134.     SetDlgItemInt(hDlg,IDD_BAUDRATE,CommData.BaudRate,FALSE);
  135.  
  136.   /* highlight the corresponding radio buttons */
  137.     CheckRadioButton(hDlg,IDD_SEVENBITS, IDD_EIGHTBITS,
  138.                 IDD_SEVENBITS + CommData.ByteSize -7);
  139.     CheckRadioButton(hDlg,IDD_NOPARITY, IDD_SPACEPARITY,
  140.                 IDD_NOPARITY + CommData.Parity - NOPARITY);
  141.     CheckRadioButton(hDlg,IDD_COM1, IDD_COM2,IDD_COM1 + CommData.Id);
  142.  
  143. }
  144.  
  145. /* set communications values; if failure, try to restore the old ones */
  146. static BOOL NEAR SetCommValues(HWND hDlg)
  147. {
  148.  
  149.     register int i, state;
  150.     short newcid;
  151.     char comstr[10];
  152.     char wintitle[50];
  153.     BYTE newid;
  154.     BYTE oldid = CommData.Id;            /* save the current values */
  155.     WORD oldbaud = CommData.BaudRate;
  156.     BYTE oldsize = CommData.ByteSize;
  157.     BYTE oldpar = CommData.Parity;
  158.     BYTE oldstop = CommData.StopBits;
  159.  
  160.   /* read the rate from the edit control */
  161.     CommData.BaudRate = GetDlgItemInt(hDlg,IDD_BAUDRATE,NULL, FALSE);
  162.  
  163.     for (i = IDD_COM1; i <= IDD_COM2; i++) {
  164.       /* get the button which is checked */
  165.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  166.     if (state) {
  167.         newid = (BYTE)(i - IDD_COM1);    /* convert to comm cid */
  168.         break;
  169.     }
  170.     }
  171.   /* read the button which is checked */
  172.     for (i = IDD_SEVENBITS; i <= IDD_EIGHTBITS; i++) {
  173.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  174.     if (state) {
  175.       /* convert to a communications byte size parameter */
  176.         CommData.ByteSize = (BYTE)(i - IDD_SEVENBITS + 7);
  177.         break;
  178.     }
  179.     }
  180.   /* read the button which is checked */
  181.     for (i = IDD_NOPARITY; i <= IDD_SPACEPARITY; i++) {
  182.     state = (int)SendDlgItemMessage(hDlg, i, BM_GETCHECK, 0, 0L);
  183.     if (state) {
  184.       /* convert to a communications parity value */
  185.         CommData.Parity = (BYTE)(i - IDD_NOPARITY + NOPARITY);
  186.         break;
  187.     }
  188.     }
  189.     if (CommData.BaudRate <= 200)    /* two stop bits in this case */
  190.     CommData.StopBits = TWOSTOPBITS;
  191.     else 
  192.     CommData.StopBits = ONESTOPBIT;
  193.  
  194.   /* if requesting a new comm port, try to open it first */
  195.     if (newid != oldid) {
  196.         LoadString(hInst, IDS_FIRSTCOM + newid, (LPSTR)comstr, sizeof(comstr));
  197.     newcid = OpenComm((LPSTR)comstr, INQUESIZE, OUTQUESIZE);
  198.       /* if successful, close the old one */
  199.     if (newcid >= 0) {
  200.         state = CloseComm(cid);
  201.       /* old one closed, so update window title and cid number */
  202.         if (state == 0) {
  203.         cid = newcid;
  204.             CommData.Id = (BYTE)cid;
  205.             LoadString(hInst,IDS_WINTITLE,(LPSTR)wintitle,sizeof(wintitle));
  206.         strcat(wintitle,comstr);
  207.         SetWindowText(MWnd.hWnd, (LPSTR)wintitle);                
  208.         }
  209.         else {
  210.           /* failed, go back to old port and show error */
  211.         CloseComm(newcid);
  212.         ShowMessage(hDlg, IDS_CANNOTOPENPORT);
  213.         return FALSE;
  214.         }
  215.     }
  216.     else {
  217.      /* failed to open new port, show error */
  218.         ShowMessage(hDlg, IDS_CANNOTOPENPORT);
  219.         return FALSE;
  220.     }
  221.     }
  222.   /* set the state of the comm DCB */
  223.     state = SetCommState((DCB FAR *)&CommData);
  224.   /* if unsuccessful, restore all the old values and show error */
  225.     if (state) {
  226.     CommData.BaudRate = oldbaud;
  227.     CommData.ByteSize = oldsize;
  228.     CommData.Parity = oldpar;
  229.     CommData.StopBits = oldstop;
  230.     SetCommState((DCB FAR *)&CommData);
  231.     SetCommInit(hDlg);
  232.         ShowMessage(hDlg, IDS_COMMSETERROR);
  233.     return FALSE;
  234.     }
  235.     return TRUE;
  236. }
  237.